package com.globefrog.example.provider.controller;

import com.globefrog.example.client.ProviderClient;
import com.globefrog.example.entity.User;
import com.globefrog.example.provider.service.UserService;
import com.globefrog.example.request.ProviderUserRequest;
import com.globefrog.example.response.ProviderUserResponse;
import com.globefrog.framework.log.LogHelper;
import com.globefrog.framework.web.BaseRestController;
import com.globefrog.framework.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : Globe-Frog
 * @date : Create in 2021/7/8
 */
@Slf4j
@RestController
public class ProviderController extends BaseRestController implements ProviderClient {

  @Autowired
  private UserService userService;
  @Autowired
  private LogHelper logHelper;

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  @ResponseBody
  @Override
  public BaseResponse<ProviderUserResponse> getById(@PathVariable("id") String id) {
    ProviderUserResponse userResponse = new ProviderUserResponse();
    User user = userService.getById(id);
    userResponse.setId(Long.valueOf(user.getId()));
    userResponse.setSex(user.getSex());
    userResponse.setName(user.getName());
    return BaseResponse.success(userResponse);
  }

  @RequestMapping(
      method = RequestMethod.POST,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE
  )
  @ResponseBody
  @Override
  public void saveUser(ProviderUserRequest request) {
    User user = new User();
    user.setName(request.getName());
    user.setAge(request.getAge());
    userService.insert(user);
  }
}
